package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> pageQuery(String name, Integer status, Integer pageNum, Integer pageSize) {

        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> nursingProjects = nursingProjectMapper.pageQuery(name, status);
        PageResponse<NursingProjectVo> nursingProjectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        return nursingProjectVoPageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        Integer count = nursingProjectMapper.selectByName(nursingProject.getName());
        if(count>0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXEIST);
        }
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo getNursingProjectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.toBean(nursingProject,NursingProjectVo.class);
    }

    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        Integer count = nursingProjectMapper.selectByNameForUpdate(nursingProject.getName(),nursingProject.getId());
        if(count>0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_EXEIST);
        }
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public void deleteNursingProject(Long id) {
        //TODO 如果护理项目中有不能删
        //如果查不到 报错
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        if (nursingProject==null){
            throw  new BaseException(BasicEnum.NURSING_PROJECT_NOT_EXEIST);
        }
        //如果状态为启用,不删
        if(nursingProject.getStatus().equals(1)){
            throw  new BaseException(BasicEnum.NURSING_PROJECT_STATUS_START);
        }
        nursingProjectMapper.delete(id);
    }

    @Override
    public List<NursingProjectVo> queryAllNursingProject() {

        List<NursingProject> lists=nursingProjectMapper.selectAll();
        List<NursingProjectVo> collect = lists.stream().map(list -> BeanUtil.toBean(list, NursingProjectVo.class)).collect(Collectors.toList());
        return  collect;
    }
}
